<?php
/*------------------------------------------------------------------------
# com_ilike - XIPAT Like Article Component
# version: 1.0
# ------------------------------------------------------------------------
# author    Duong Tien Dung - www.xipat.com
# copyright Copyright (C) 2013 www.xipat.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.xipat.com
# Technical Support:  http://www.xipat.com/support.html
-------------------------------------------------------------------------*/
// no direct access
defined('_JEXEC') or die;

JHtml::addIncludePath(JPATH_COMPONENT.'/helpers/html');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.multiselect');
$user        = JFactory::getUser();
$userId        = $user->get('id');
$listOrder    = $this->escape($this->state->get('list.ordering'));
$listDirn    = $this->escape($this->state->get('list.direction'));
$canOrder    = $user->authorise('core.edit.state', 'com_xp_like.category');
$saveOrder    = $listOrder=='ordering';    
include(JPATH_COMPONENT.'/Chart.php');
$chart = new Chart('LineChart');    
// graph day                                    
$data = array(
        'cols' => array(
                array('id' => '', 'label' => 'Year', 'type' => 'string'),
                array('id' => '', 'label' => 'Like', 'type' => 'number'),
                array('id' => '', 'label' => 'Dislike', 'type' => 'number')
        ),
        'rows' => array(  
                array('c' => array(array('v' => date('M d',strtotime("-3 day"))), array('v' => $this->day3->like), array('v' => $this->day3->dislike))),
                array('c' => array(array('v' => date('M d',strtotime("-2 day"))), array('v' => $this->day2->like), array('v' => $this->day2->dislike))),    
                array('c' => array(array('v' => date('M d',strtotime("-1 day"))), array('v' => $this->day1->like), array('v' => $this->day1->dislike))), 
                array('c' => array(array('v' => date('M d', time())), array('v' => $this->status->like), array('v' => $this->status->dislike))),
              //  array('c' => array(array('v' => date('M d',strtotime("+1 day"))), array('v' => 0), array('v' => 0)))
        )
);  

$chart->load(json_encode($data));
$options = array('title' => 'Report', 'theme' => 'maximized', 'width' => 600, 'height' => 200);
echo $chart->draw('day', $options);

// graph week
$week = array(
        'cols' => array(
                array('id' => '', 'label' => 'Year', 'type' => 'string'),
                array('id' => '', 'label' => 'Like', 'type' => 'number'),
                array('id' => '', 'label' => 'Dislike', 'type' => 'number')
        ),
        'rows' => array(  
                array('c' => array(array('v' => date('F j',strtotime('-14 days'))), array('v' => $this->week2->like), array('v' => $this->week2->dislike))),    
                array('c' => array(array('v' => date('M d',strtotime('-7 days'))), array('v' => $this->week1->like), array('v' => $this->week1->dislike))), 
                array('c' => array(array('v' => date('M d', time())), array('v' => $this->week->like), array('v' => $this->week->dislike))),
        )
);  

$chart->load(json_encode($week));
$options = array('title' => 'Report', 'theme' => 'maximized', 'width' => 600, 'height' => 200);
echo $chart->draw('week', $options);

// graph month 
$date = date("Y-m");
$month = array(
        'cols' => array(
                array('id' => '', 'label' => 'Year', 'type' => 'string'),
                array('id' => '', 'label' => 'Like', 'type' => 'number'),
                array('id' => '', 'label' => 'Dislike', 'type' => 'number')
        ),
        'rows' => array(  
                array('c' => array(array('v' => date('M Y', (strtotime($date) - (2400000 * 2)))), array('v' => $this->month2->like), array('v' => $this->month2->dislike))), 
                array('c' => array(array('v' => date('M Y', (strtotime($date) - 2400000))), array('v' => $this->month1->like), array('v' => $this->month1->dislike))), 
                array('c' => array(array('v' => date('M Y', strtotime($date))), array('v' => $this->month->like), array('v' => $this->month->dislike)))
        )
);  
$chart->load(json_encode($month));

$options = array('title' => 'Report', 'theme' => 'maximized', 'width' => 600, 'height' => 200);
echo $chart->draw('month', $options); 
// demonstration of pie chart and simple array
$chart = new Chart('PieChart');
$countuser = (int)$this->countuser;
$countguest = (int)$this->countguest;
$data = array(
        array('user', 'slices'),
        array('Loged in', $countuser),
        array('Guest', $countguest)
);
$chart->load($data, 'array');

$options = array('title' => 'Report user/guest in month', 'is3D' => true, 'width' => 400, 'height' => 250);
echo $chart->draw('user-gest', $options);
  
 
?>     
<script type="text/javascript">
    $(function() {
        $( "#tabs" ).tabs();
    });
</script>
<form action="<?php echo JRoute::_('index.php?option=com_ilike&view=xplikes'); ?>" method="post" name="adminForm" id="adminForm">   
<div id="j-main-container">
    <div id="tabs">
        <ul>
            <li><a href="#tabs-1"><?php echo JText::_('COM_XP_LIKE_DAY'); ?></a></li>
            <li><a href="#tabs-2"><?php echo JText::_('COM_XP_LIKE_WEEK'); ?></a></li>
            <li><a href="#tabs-3"><?php echo JText::_('COM_XP_LIKE_MONTH'); ?></a></li>
        </ul>
        <div id="tabs-1">
            <div id="day"></div>
        </div>
        <div id="tabs-2">
            <div id="week"></div> 
        </div>
        <div id="tabs-3">
            <div id="month"></div> 
        </div>        
    </div>
    <div id="user-gest"></div>
    <div class="clr"> </div> 
    <div id="filter-bar" class="btn-toolbar">
    <fieldset id="filter-bar">
        <div class="filter-search fltlft">
            <label class="filter-search-lbl" for="filter_search"><?php echo JText::_('JSEARCH_FILTER_LABEL'); ?></label>
            <input type="text" name="filter_search" id="filter_search" value="<?php echo $this->escape($this->state->get('filter.search')); ?>" title="<?php echo JText::_('COM_XP_LIKE_SEARCH_IN_TITLE'); ?>" />
            <button type="submit"><?php echo JText::_('JSEARCH_FILTER_SUBMIT'); ?></button>
            <button type="button" onclick="document.id('filter_search').value='';this.form.submit();"><?php echo JText::_('JSEARCH_FILTER_CLEAR'); ?></button>
        </div>      
        <div class="filter-select fltrt">
            <select name="filter_component" class="inputbox" onchange="this.form.submit()">
                <option value=""><?php echo JText::_('Select Component');?></option>
                <?php echo JHtml::_('select.options', $this->component, 'value', 'text', $this->state->get('filter.component'));?>  
            </select>
        </div>
    </fieldset>
    <div class="clr"> </div>        
        <?php $total= $this->status->like+ $this->status->dislike; 
        if ($total == 0) $plike=$pdislike=0;
        else {
            $plike=($this->status->like/$total)*100;
            $pdislike=($this->status->dislike/$total)*100;
        }
        ?>    
    </div>
    <div class="clr" style="clear:both"> </div> 
    <div>
        <?php echo sprintf(JText::_('COM_XP_LIKE_TOTAL_TIME'), $this->status->like, $this->status->dislike, $this->status->content, $this->from, $this->to); ?>
    </div>
    <div class="clr"> </div>

    <table class="adminlist">
        <thead>
            <tr>
                <th width="1%">
                    <input type="checkbox" name="checkall-toggle" value="" title="<?php echo JText::_('JGLOBAL_CHECK_ALL'); ?>" onclick="Joomla.checkAll(this)" />
                </th>
                <th width="1%" class="nowrap">
                    <?php echo JHtml::_('grid.sort', 'JGRID_HEADING_ID', 'id', $listDirn, $listOrder); ?>
                </th>
                
                
                <th width="10%">
                    <?php echo JHtml::_('grid.sort',  'COM_XP_LIKE_HEADING_CONTENT_ID', '#__xplike.content_id', $listDirn, $listOrder); ?>
                </th>
                <th>
                    <?php echo JText::_('COM_XP_LIKE_CONTENT_TITLE'); ?>
                </th>
                <th>
                    <?php echo JHtml::_('grid.sort',  'COM_XP_LIKE_HEADING_COMPONENT', '#__xplike.component', $listDirn, $listOrder); ?>
                </th>                
                <th>
                    <?php echo JText::_('COM_XP_LIKE_HEADING_LIKES'); ?><br /> 
                    <?php echo JText::_('COM_XP_LIKE_HEADING_DISLIKES'); ?> 
                </th>
                <th>
                    <?php echo JText::_('COM_XP_LIKE_VOTE_TOTAL'); ?>
                </th>                 
                <th>
                    <?php echo JText::_('COM_XP_LIKE_CONTENT_HITS'); ?>
                </th>  
                <th>
                    <?php echo JText::_('COM_XP_LIKE_CONTENT_PUBLISH_DATE'); ?>
                </th>                               
                
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td colspan="13">
                    <?php echo $this->pagination->getListFooter(); ?>
                </td>
            </tr>
        </tfoot>
        <tbody>
        <?php foreach ($this->items as $i => $item) :    
            $ordering    = ($listOrder == 'ordering');
            $canCreate    = $user->authorise('core.create');
            $canEdit    = $user->authorise('core.edit');
            $canCheckin    = $user->authorise('core.manage',        'com_checkin') || $item->checked_out == $userId || $item->checked_out == 0;
            $canChange    = $user->authorise('core.edit.state') && $canCheckin;
            ?>
            <tr class="row<?php echo $i % 2; ?>">
                <td class="center">
                    <?php echo JHtml::_('grid.id', $i, $item->id); ?>
                </td>
                <td class="center">
                    <?php if ($item->checked_out) : ?>
                        <?php echo JHtml::_('jgrid.checkedout', $i, $item->editor, $item->checked_out_time, 'xplikes.', $canCheckin); ?>
                    <?php endif; ?>
                    <?php if ($canEdit) : ?>
                        <a href="<?php echo JRoute::_('index.php?option=com_ilike&task=xplike.edit&id='.(int) $item->id); ?>">
                            <?php echo $this->escape($item->id); ?></a>
                    <?php else : ?>
                            <?php echo $this->escape($item->id); ?>
                    <?php endif; ?>
                </td>
                
                
                <td width="5%" class="center">
                    <?php echo $this->escape($item->content_id); ?>
                </td>
                <td>
                    <?php if ($item->component == 'com_content') echo $this->escape($item->title); ?>
                </td>
                <td class="center">
                    <?php echo $this->escape($item->component); ?>
                </td>
                <td width="10%" class="left">
                    <?php
                       $vote =  $this->escape($item->likes) + $this->escape($item->dislikes);
                        if ($vote == 0) $plike=$pdislike=0;
                        else {
                            $plike=($item->likes/$vote)*100;
                            $pdislike=($item->dislikes/$vote)*100;
                        }                    
                    ?>
                    <p class="vote"><span class="number"><?php echo $this->escape($item->likes); ?></span><span class="vote"><span class="like" style="width: <?php echo $plike ? $plike : 2; ?>%;">&nbsp</span></span></p>
                    <p class="vote"><span class="number"><?php echo $this->escape($item->dislikes); ?></span><span class="vote"><span class="dislike" style="width: <?php echo $pdislike ? $pdislike : 2; ?>%;">&nbsp</span></span></p> 
                </td>
                <td class="center">
                    <?php echo $this->escape($item->likes) + $this->escape($item->dislikes); ?>
                </td>                 
                <td class="center">
                    <?php echo $this->escape($item->hits); ?>
                </td>  
                <td class="center">
                    <?php echo $this->escape($item->publish_date); ?>
                </td>                               
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>


        <input type="hidden" name="task" value="" />
        <input type="hidden" name="boxchecked" value="0" />
        <input type="hidden" name="filter_order" value="<?php echo $listOrder; ?>" />
        <input type="hidden" name="filter_order_Dir" value="<?php echo $listDirn; ?>" />
        <?php echo JHtml::_('form.token'); ?>
</div>
</form>
